<%@ page trimDirectiveWhitespaces="true" %>
<%@page import="com.jason.imagesnode.MyStringUtil" %>
<%@page import="org.json.JSONObject" %>
<%@page import="java.util.UUID" %>
<%@ page import="java.util.Date" %>
<%@ page import="java.text.SimpleDateFormat" %>

<%@ page import="java.util.HashMap" %>
<%@ page import="java.util.Map" %>
<%@ page import="com.google.zxing.common.BitMatrix" %>
<%@ page import="com.google.zxing.MultiFormatWriter" %>
<%@ page import="com.google.zxing.BarcodeFormat" %>
<%@ page import="com.google.zxing.EncodeHintType" %>
<%@ page import="com.jason.imagesnode.MatrixToImageWriter" %>

<%@ page import="java.util.Iterator" %>
<%@ page import="java.util.List" %>
<%@ page contentType="text/html; charset=UTF-8" %>

<%@ page import="sun.misc.BASE64Decoder" %>
<%@ page import="java.io.FileOutputStream" %>
<%@ page import="java.io.File" %>
<%@ page import="java.io.BufferedReader" %>
<%@ page import="java.io.InputStreamReader" %>
<%@ page import="com.alibaba.fastjson.JSON" %>
<%@ page import="com.alibaba.fastjson.JSONArray" %>
<%@ page import="com.jason.imagesnode.WriteQrCode" %>

<%
    String sId = UUID.randomUUID().toString();
    String fileName = "";

    response.setHeader("Access-Control-Allow-Origin", "*");

    JSONObject obj = new JSONObject();
    obj.put("result", "Y");
    boolean flag = true;

    try {
        request.setCharacterEncoding("UTF-8");

        String postfixName = (String) request.getParameter("postfixName");// 服务器前缀
        if (MyStringUtil.isBlank(postfixName)) {
            postfixName = "";
        }


        //String content = (String) request.getParameter("content");
        BufferedReader br = request.getReader();
        String str = "";
        String listString = "";
        while ((str = br.readLine()) != null) {
            listString += str;
        }

        Object o = JSON.parseArray(listString);
        List<Map<String, String>> newobjectParam = (List<Map<String, String>>) o;

        //二维码内容
        String new_pro_code = newobjectParam.get(0).get("new_pro_code");
        //投用日期
        String start_use_time = newobjectParam.get(0).get("start_use_time");
        //资产名称
        String pro_name = newobjectParam.get(0).get("pro_name");

        //获取文件路径
        String basePath = request.getServletContext().getRealPath("/");
        String filename = UUID.randomUUID().toString().replace("-", "");
        filename = MyStringUtil.getFileName(filename, postfixName);
        String filepath = MyStringUtil.getFilePath(filename);
        File f = new File(basePath + filepath);
        if (!f.exists()) {
            f.mkdirs();
        }

        String filenames = filename + ".jpg";// 文件名
        String path = basePath + filepath + "/" + filename + ".jpg";// 文件绝对路径

        /*String title = "资产名称：" + pro_name;
        String new_pro_code_text = "资产编号：" + new_pro_code;
        String text2 = "购置时间：" + start_use_time;*/
        String title = pro_name;
        String new_pro_code_text = new_pro_code;
        String text2 = start_use_time;
      /*  MultiFormatWriter multiFormatWriter = new MultiFormatWriter();

        Map hints = new HashMap();
        hints.put(EncodeHintType.CHARACTER_SET, "UTF-8");
        BitMatrix bitMatrix = multiFormatWriter.encode(content, BarcodeFormat.QR_CODE, 400, 400, hints);
        File file1 = new File(path);
        MatrixToImageWriter.writeToFile(bitMatrix, "jpg", file1);*/
        //测试
        //String logoPath = "D:/qrcodeTest";
        String logoPath = basePath;
        String logoName = newobjectParam.get(0).get("logoName");
        //绘制二维码
        //WriteQrCode.writeTextAndImageCode(new_pro_code, title, new_pro_code_text, text2, path, logoPath, logoName);
        WriteQrCode.writeTextCode(new_pro_code, title, new_pro_code_text, text2, path);
        System.out.println(basePath + filepath + "/" + filename + ".jpg");

        obj.put("filename", filenames);
    } catch (Exception e) {
        obj.put("errMsg", e);
        obj.put("result", "N");
        e.printStackTrace();
    } finally {
        try {

        } catch (Exception e) {
        }
        //out.println("Access-Control-Allow-Origin:*\r\n");
        String strObj = obj.toString();
        out.println(strObj);
    }
%>
